<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Think\Page;
class ShopsController extends AdminController {
    public function _initialize(){
        parent::_initialize();
    }
	public function index(){
		$type = I('type')?I('type'):null;
		$shop_search = I('search');
		if($shop_search){
		    $where['shop_name'] = array('like',"%".$shop_search."%");
		}
		if(!empty($type)){
			$where['type_id']=$type;
		}
		$count=M('Shop s')->join('yang_shop_type st ON s.type_id=st.typeid')->where($where)->count();
		$Page=new Page($count,10);
		//setPageParameter($Page, array('account'=>$account));
		$show       = $Page->show();
		$list =  M('Shop s')->field("s.*,st.*,s.is_show as isshow")->join('yang_shop_type st ON s.type_id=st.typeid')->where($where)
		->limit($Page->firstRow.','.$Page->listRows)->select();
		$type_pid = M('Shop_type')->where(array('pid'=>'0'))->select();
		$this->assign('type_pid',$type_pid);
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}
	public function getTypeid(){
		$pid = I('pid');
		$type_id = M('Shop_type')->where(array('pid'=>$pid))->select();
		$this->ajaxReturn($type_id);
	}
	public function details(){
        $shop_id = I('shop_id');
        $list = M('Shop')->where(array('shop_id'=>$shop_id))->find();
        $type_pid_name = M("Shop_type")->where(array('typeid'=>$list['type_pid']))->find();
        $type_name = M("Shop_type")->where(array('typeid'=>$list['type_id']))->find();
        $this->assign('type_name',$type_name);
        $this->assign('type_pid_name',$type_pid_name);
        $this->assign('list',$list);
        $pid = M("Shop_type")->where(array('pid'=>"0"))->select();
        $this->assign('pid',$pid);
        $this->display();
	}
	public function addShops(){
		$shop_id = I('shop_id');
		$data['shop_name'] = I('shop_name');
		$data['content'] = I('content');
	    if($_FILES["image"]['name']){
 			$data['image']=$this->upload($_FILES["image"]);
		}
		$data['url'] = I('url');
		$data['address'] = I('address');
		$data['phone'] = I('phone');
		$type_pid = I('type_pid');
		if($type_pid != 0 | $type_pid !=0){
		    $data['type_id'] = I('type_id');
		    $data['type_pid'] = I('type_pid');
		}
		if($shop_id){
			$re = M("Shop")->where(array('shop_id'=>$shop_id))->save($data);
		}else{
			$re =  M("Shop")->add($data);
		}
	   if($re){
	    	$this->success('操作成功');
	    }else{
	        $this->error('操作失败');
	    }
	}
	public function shopDel(){
	    $shop_id = I('shop_id');
	    $list = M('Shop')->where(array('shop_id'=>$shop_id))->delete();
	    if($list){
	    	$this->success('操作成功');
	    }else{
	        $this->error('操作失败');
	    }
	}
	

	public function typeIndex(){
	    $pid = M('Shop_type')->where(array('pid'=>'0'))->select();
	    $this->assign('pid',$pid);
		$this->display();
	}
	public function toEdit(){
		$pid = I('typeid');
		$pid_name =  M('Shop_type')->where(array('typeid'=>$pid))->find();
		$type_name = M('Shop_type')->where(array('pid'=>$pid))->select();
		$this->assign('pid_name' ,$pid_name );
		$this->assign('type_down' ,$type_name );
		$this->display();
	}
	public function toAddType(){
		$this->display();
	}
	public function addTypePid(){
	    $data['type_name'] = I('pid_name');
	    $data['pid'] = 0;
	    $data['is_show'] = 1;
	    if(!$data['type_name']){
	        $this->error('操作失败');
	    }
	    $re = M('Shop_type')->add($data);
	    if($re){
	        $this->success('操作成功');
	    }else{
	        $this->error('操作失败');
	    }
	}
	public function editType(){
		$typeid = I('typeid');
		$type_name = I('type_name');
		$re =  M('Shop_type')->where(array('typeid'=>$typeid))->save(array('type_name'=>$type_name));
		if($re){
		    $data['info'] = "修改成功";
		    $data['status'] = 1;
		    $this->ajaxReturn($data);
		}else{
		    $data['info'] = "修改失败";
		    $data['status'] = -1;
		    $this->ajaxReturn($data);
		}
	}
	public function addType(){
	    $dat['pid'] = I('pid');
	    $dat['type_name'] = I('type_name');
	    $dat['is_show'] = 1;
	    $re =  M('Shop_type')->add($dat);
	    if($re){
	        $data['info'] = "添加成功";
	        $data['status'] = 1;
	        $this->ajaxReturn($data);
	    }else{
	        $data['info'] = "添加失败";
	        $data['status'] = -1;
	        $this->ajaxReturn($data);
	    }
	}
	public function shop_typedel(){
		$typeid = I('typeid');
		$re =  M('Shop_type')->where(array('typeid'=>$typeid))->find();
		if($re['pid'] == 0){
		    $re1  =  M('Shop_type')->where(array('typeid'=>$typeid))->delete();
		    $re2  =  M('Shop_type')->where(array('pid'=>$typeid))->find();
		    if($re2){
		        $res =  M('Shop_type')->where(array('pid'=>$typeid))->delete();
		        if($res){
		        	$res = M('Shop')->where(array('type_id'=>$typeid))->delete();
		        }
		    }else{
		    	$res = $re1;
		    }
		    
		}else{
		    $res =  M('Shop_type')->where(array('typeid'=>$typeid))->delete();
		    
		    if($res){
		        $res = M('Shop')->where(array('type_id'=>$typeid))->delete();
		    }
		}
		if($res){
		    $data['info'] = "删除成功";
		    $data['status'] = 1;
		    $this->ajaxReturn($data);
		}else{
		    $data['info'] = "删除失败";
		    $data['status'] = -1;
		    $this->ajaxReturn($data);
		}
	}
	public function shop_typeopen(){
	    $typeid = I('typeid');
	    $res =  M('Shop_type')->where(array('typeid'=>$typeid))->save(array('is_show'=>1));
	    if($res){
	        $data['info'] = "操作成功";
	        $data['status'] = 1;
	        $this->ajaxReturn($data);
	    }else{
	        $data['info'] = "操作失败";
	        $data['status'] = -1;
	        $this->ajaxReturn($data);
	    }
	}
	public function shop_typeclose(){
	    $typeid = I('typeid');
	    $res =  M('Shop_type')->where(array('typeid'=>$typeid))->save(array('is_show'=>0));
	    if($res){
	        $data['info'] = "操作成功";
	        $data['status'] = 1;
	        $this->ajaxReturn($data);
	    }else{
	        $data['info'] = "操作失败";
	        $data['status'] = -1;
	        $this->ajaxReturn($data);
	    }
	}
	public function shop_open(){
	    $shop_id = I('shop_id');
	    $res =  M('Shop')->where(array('shop_id'=>$shop_id))->save(array('is_show'=>1));
	    if($res){
	        $data['info'] = "操作成功";
	        $data['status'] = 1;
	        $this->ajaxReturn($data);
	    }else{
	        $data['info'] = "操作失败";
	        $data['status'] = -1;
	        $this->ajaxReturn($data);
	    }
	}
	public function shop_close(){
	    $shop_id = I('shop_id');
	    $res =  M('Shop')->where(array('shop_id'=>$shop_id))->save(array('is_show'=>0));
	    if($res){
	        $data['info'] = "操作成功";
	        $data['status'] = 1;
	        $this->ajaxReturn($data);
	    }else{
	        $data['info'] = "操作失败";
	        $data['status'] = -1;
	        $this->ajaxReturn($data);
	    }
	}
	public function checkType(){
		$typeid = I('typeid');
		$res =  M('Shop_type')->where(array('pid'=>$typeid))->select();
		if($typeid  == 0) $res = null ;
		
		$this->ajaxReturn($res);
	}
	//多图管理
	public function images(){
	    $goods_id = I('goods_id')?I('goods_id'):null;
	    $pics = M('Shops_pic')->where(array('shops_id'=>$goods_id))->select();
	    $this->assign('pics',$pics);
	    $goods = M('Shop')->where(array('shop_id'=>$goods_id))->find();
	    session('shops_id',$goods_id);
	    $this->assign('goods_id',$goods_id);
	    $this->assign('goods',$goods);
	    $this->display();
	}
	
	public function getImages(){
	    $url = $this->imgUpload($_FILES['file']);
	    $data = [
	    'shops_id'=>session('shops_id'),
	    'pic'=>$url,
	    'add_time'=>time(),
	    ];
	    M('Shops_pic')->add($data);
	    //M('goods')->add(['pics'=>$_FILES['file']['name']]);
	    //dump($_FILES);die;
	}
	public function deletePic(){
	    $goods_pic_id = I('goods_pic_id');
	    $goods_id = I('goods_id');
	    $pic = M('shops_pic')->where(array('shops_pic_id'=>$goods_pic_id))->find();
	    //dump('.'.$pic['pic']);
	    $s = unlink('.'.$pic['pic']);
	    //dump($s);die;
	    $r = M('Shops_pic')->delete($goods_pic_id);
	    $this->success('删除成功',U('Shops/images',array('goods_id'=>$goods_id)));
	}
}